package eleanor.ltd.isRealEstate.dao.messages;

import java.util.List;

import javax.inject.Inject;

import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import eleanor.ltd.isRealEstate.entities.messages.Messages;
import eleanor.ltd.isRealEstate.entities.messages.priority.MessagePriority;

public class MessagesDAOImpl implements MessagesDAO {

	
	@Inject
    private Session session;

    public boolean save(Messages newMessage) {
            if (newMessage != null) {
                    session.save(newMessage);
                    return true;
            }
            return false;
    }

    public boolean delete(Messages Message) {
            if (Message != null) {
                    session.delete(Message);
                    return true;
            }
            return false;
    }

    public boolean update(Messages Message) {
            if (Message != null) {
                    session.update(Message);
                    return true;
            }
            return false;
    }

    public List<Messages> findAll() {
            return session.createCriteria(Messages.class)
                            .addOrder(Order.asc("name")).list();
    }

    public Messages findByID(Long id) {
            if (id != null) {
                    return (Messages) session.load(Messages.class, id);
            }
            return null;
    }

    public boolean deleteByID(Long id) {
            if (id != null) {
                    Messages m = findByID(id);
                    if (m != null) {
                            session.delete(m);
                            return true;
                    }
                    return false;
            }
            return false;
    }

    public List<Messages> findAllForPriority(MessagePriority messagePriority) {
            if (messagePriority!=null) {
                    return (List<Messages>) session.createCriteria(Messages.class).add(Restrictions.eq("messagePriority.id", messagePriority.getId()));
            }
            return null;
            
    }

}
